package com.test05;

import java.util.*;

/**
 * @author south wind
 * @version 1.0
 * @date 2025/3/31 22:34
 * @description
 */
public class DoudizhuCard {
    public static void main(String[] args) {
        // 初始化 54 张牌
        List<Card> deck = initializeDeck();
        // 洗牌
        Collections.shuffle(deck);

        // 创建三名玩家
        Player player1 = new Player(1, "玩家1");
        Player player2 = new Player(2, "玩家2");
        Player player3 = new Player(3, "玩家3");
        List<Player> players = Arrays.asList(player1, player2, player3);

        // 随机选择地主
        Random random = new Random();
        int landlordIndex = random.nextInt(3);
        players.get(landlordIndex).setLandlord(true);

        // 发牌
        dealCards(deck, players);

        // 输出每个玩家的牌
        for (Player player : players) {
            System.out.println(player);
        }
    }

    // 初始化 54 张牌
    public static List<Card> initializeDeck() {
        List<Card> deck = new ArrayList<>();
        String[] suits = {"♠", "♥", "♣", "♦"};
        String[] ranks = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
        int id = 1;

        // 普通牌
        for (String suit : suits) {
            for (String rank : ranks) {
                deck.add(new Card(suit, rank, id++));
            }
        }

        // 大小王
        deck.add(new Card(null, "小王", id++));
        deck.add(new Card(null, "大王", id));

        return deck;
    }

    // 发牌
    public static void dealCards(List<Card> deck, List<Player> players) {
        int playerCount = players.size();
        int index = 0;
        // 轮流发牌
        // 遍历牌堆，为每个玩家分配卡片
        for (; index < deck.size() - 3; index++) {
            // 根据当前索引和玩家数量取模，决定将卡片添加到哪个玩家的手牌中
            players.get(index % playerCount).addCard(deck.get(index));
        }
        // 给地主发底牌
        for (Player player : players) {
            if (player.isLandlord()) {
                for (; index < deck.size(); index++) {
                    player.addCard(deck.get(index));
                }
                break;
            }
        }
    }
}
